package demo_factory;

/**
 * 工厂模式demo
 */
public class demo001 {
    public static void main(String[] args) {
        Car car = new CarFactory().getCar("五菱");
        System.out.println(car.getName());
    }
    /**
     * 测类对象
     */
    interface Car {
        public String getName();
    }
    static class  Wuling implements Car{
        @Override
        public String getName() {
            return "五菱";
        }
    }
    static class Tesile implements Car{
        @Override
        public String getName() {
            return "特斯拉";
        }
    }

    /**
     * 工厂类
     */
    static class CarFactory{
        public Car getCar(String key) {
            if ("五菱".equals(key)) {
                return new Wuling();
            } else if ("特斯拉".equals(key)) {
                return new Tesile();
            }
            return null;
        }
    }
}
